package com.rzico.sbl.ui.mine

import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityInfoPasswordBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.viewmodel.LoginViewModel
import com.xinnuo.common.extend.content
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common_ui.base.BaseActivity

class InfoPasswordActivity : BaseActivity(R.layout.activity_info_password) {

    private val mBinding by lazy { ActivityInfoPasswordBinding.bind(getRootView()) }

    override fun getViewModel() = get<LoginViewModel>()

    override fun initData() {
        initTitle(title = "修改密码")
    }

    override fun initListener() {
        mBinding.apply {
            btSure.oneClick {
                when {
                    pwdNow.text.isBlank() -> {
                        pwdNow.requestFocus()
                        showToast("请输入原密码")
                        return@oneClick
                    }
                    pwdNew.text.isBlank() -> {
                        pwdNew.requestFocus()
                        showToast("请输入新密码")
                        return@oneClick
                    }
                    pwdConfirm.text.isBlank() -> {
                        pwdConfirm.requestFocus()
                        showToast("请再次输入新密码")
                        return@oneClick
                    }
                    pwdNow.text.length < 6 -> {
                        pwdNow.requestFocus()
                        showToast("原密码长度不少于6位")
                        return@oneClick
                    }
                    pwdNew.text.length < 6 -> {
                        pwdNew.requestFocus()
                        showToast("新密码长度不少于6位")
                        return@oneClick
                    }
                    pwdConfirm.text.length < 6 -> {
                        pwdConfirm.requestFocus()
                        showToast("确认密码长度不少于6位")
                        return@oneClick
                    }
                    pwdNew.content != pwdConfirm.content -> {
                        showToast("新密码和确认密码不一致")
                        return@oneClick
                    }
                }

                // 获取公钥
                getViewModel()
                    .getPublicKey {
                        window
                            .decorView
                            .post { pwdChange() }
                    }
            }
        }
    }

    /**
     * 修改密码
     */
    private fun pwdChange() {
        getViewModel()
            .request(
                isLoadingBefore = false,
                requestUrl = BaseUrl.pwd,
                params = arrayOf(
                    "oldPassword" to getViewModel().encrypt(mBinding.pwdNow.content),
                    "newPassword" to getViewModel().encrypt(mBinding.pwdNew.content)
                ),
                onSuccess = {
                    showToast("修改密码成功！")
                    finishView()
                }
            )
    }

}